import { SET_USER, LOGOUT } from './types';

// 定义用户状态的类型
export interface UserState {
  token: string | null; // 存储 token
  info: any | null; // 存储用户信息，包含 name 和 email
}

// 定义初始状态
const initialState: UserState = {
  token: null,
  info: null,
};

// 定义用户 reducer
const userReducer = (state = initialState, action: any): UserState => {
  switch (action.type) {
    case SET_USER:
      return { 
        ...state, 
        token: action.payload.token, // 存储 token
        info: action.payload.info // 存储用户信息
      };
    case LOGOUT:
      return initialState; // 登出时重置状态
    default:
      return state;
  }
};

export default userReducer;